﻿//Copyright 2011 University of Southampton 
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
using SharePointEntityProject.Properties;
using Microsoft.SharePoint.Taxonomy;
using UniversityOfSouthampton.Taxonomy;

namespace SharePointEntityProject.EntityEditorLib
{




    public class FieldManagementLib : IDisposable
    {
        private string SITE_URL;
        private SPSite site;
        private SPWeb web;
        public FieldManagementLib(string SITE_URL)
        {

            this.SITE_URL = SITE_URL;
            this.site = new SPSite(this.SITE_URL);//Dispose
            this.web = site.OpenWeb();//Dispose



        }


        /// <summary>
        /// Disposes of all SP objects that were used. 
        /// </summary>
        public void Dispose()
        {
            web.Update();
            this.web.Dispose();
            this.site.Dispose();
        }


        public void CreateNewField(FieldInfo fieldInfo)
        {

            ////Create a new field
            //SPField field = web.Fields.CreateNewField(fieldInfo.Type, fieldInfo.Name);


            //field.Description = fieldInfo.Description;
            //field.Group = Properties.Resources.DefaultEntityEditorFieldTypeGroupName;

            //web.Fields.Add(field);
            //web.Update();

        }

        public void GetAllFieldTypes()
        {
            List<string> DisabledFieldTypes = SharePointEntityProject.Properties.Resources.DisabledFieldTypesList.Split(';').ToList<string>();

            SPFieldTypeDefinitionCollection typeCollection  = web.FieldTypeDefinitionCollection;
            foreach (SPFieldTypeDefinition curType in typeCollection)
            {
                //Check not disabled
                if (DisabledFieldTypes.Contains(curType.TypeName))
                {
                    Debug.WriteLine(String.Format("Disabled type: {0}  {1}", curType.TypeName, curType.TypeDisplayName));
                    //TODO: return 
                }
                else
                {
                    Debug.WriteLine(String.Format("Type: {0}    {1}", curType.TypeName, curType.TypeDisplayName));
                }
            }

        }


    }

}
